package com.baiytfp.hf.HFDataExchange.action.controller.service;

import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.LocaleEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.alibaba.fastjson.JSONObject;
import com.baiytfp.hf.HFDataExchange.common.base.anotation.Record;
import com.baiytfp.hf.HFDataExchange.common.base.shiro.DeviceSessionControlFilter;
import com.baiytfp.hf.HFDataExchange.common.utils.WebRandomCodeUtils;
import com.baiytfp.hf.baiytfp.framework.base.BaseController;
import com.baiytfp.hf.baiytfp.framework.model.state.LoginResultType;
import com.baiytfp.hf.baiytfp.framework.shiro.ShiroUtil;
import com.baiytfp.hf.baiytfp.framework.shiro.listener.UserSessionListener;
import com.baiytfp.hf.baiytfp.framework.util.UserUtil;
import com.baiytfp.hf.baiytfp.hfutils.HFLog;
import com.baiytfp.hf.baiytfp.hfutils.HFRequest;
import com.baiytfp.hf.baiytfp.hfutils.HFResponse;
import com.baiytfp.hf.baiytfp.hfutils.HFWeb;

@Controller
@RequestMapping("/service/index")
public class IndexSController extends BaseController {

	public IndexSController() {

	}

	//登录
	@RequestMapping(value="/login", method= RequestMethod.POST)
	@Record
	public ModelAndView login(HttpServletRequest request,HttpServletResponse response) throws Exception {

//		System.out.println("密码加密: "+PropertiesUtil.encryptDes("Hf123456"));
//		System.out.println("密码解密: "+PropertiesUtil.decryptDes("v1sk6EjJFdDpKaWpBJTW17=="));
//		System.out.println("密码解密: "+PropertiesUtil.decryptDes("fTBiTafvbtE="));
		
		String user_code = HFWeb.getIstance().getUserCode();

		if("".equals(user_code)){
			
			//验证码
			if( !WebRandomCodeUtils.checkCode(HFRequest.getIstance().getString(request, "random_code"), request)) {
				HFResponse.getIstance().returnResult(response, LoginResultType.VERIFICATION_CODE_ERROR.getKey(), LoginResultType.VERIFICATION_CODE_ERROR.getValue(), null);
				return null;
			}
			
			user_code = HFRequest.getIstance().getString(request, "j_username");
			String password = HFRequest.getIstance().getString(request, "j_password");

			//登录系统
			int login_result = HFWeb.getIstance().login(user_code, password);

			if(login_result == LoginResultType.SUCCESS.getKey()){	//保存用户code
				Map user = UserUtil.getUserInfo(ShiroUtil.getUserCode());
				new HFLog(HFLog.TYPE_LOGIN).loginToDB((String)user.get("user_code"),(String)user.get("user_name"));
				request.getSession().setAttribute(UserSessionListener.SHIRO_USER_SESSION_KEY, user);
				//返回成功
				HFResponse.getIstance().returnSuccess(response);
				
			}else if(login_result == LoginResultType.UNKNOWN_ACCOUNT.getKey()){
				HFResponse.getIstance().returnResult(response, LoginResultType.UNKNOWN_ACCOUNT.getKey(), LoginResultType.UNKNOWN_ACCOUNT.getValue(), null);
			}else if(login_result == LoginResultType.INCORRECT_CREDENTIALS.getKey()){
				HFResponse.getIstance().returnResult(response,  LoginResultType.INCORRECT_CREDENTIALS.getKey(), LoginResultType.INCORRECT_CREDENTIALS.getValue(), null);
			}else if(login_result == LoginResultType.PASSWOR_EXPIRE.getKey()){
				HFResponse.getIstance().returnResult(response, LoginResultType.PASSWOR_EXPIRE.getKey(), LoginResultType.PASSWOR_EXPIRE.getValue(), null);
			}else if(login_result == LoginResultType.FAILURE.getKey()){
				HFResponse.getIstance().returnResult(response, LoginResultType.FAILURE.getKey(), LoginResultType.FAILURE.getValue(), null);
			}
		} else {	//已经是登陆状态了
			HFResponse.getIstance().returnResult(response, -5, "已经是登陆状态", null);
		}

		return null;
	}
	
	//退出
	@RequestMapping("/logout")
	public ModelAndView logout(HttpServletRequest request,HttpServletResponse response) throws Exception {
		try {
			DeviceSessionControlFilter.cleanDevice(ShiroUtil.getUserCode());
			HFWeb.getIstance().logout(request);
		}catch (Exception e) {
			//ignore exception
		}
		HFResponse.getIstance().returnSuccess(response);
		return null;
	}


	//变更语言
	@RequestMapping("/setlang")
	public ModelAndView setlang(HttpServletRequest request,HttpServletResponse response) throws Exception {

		String new_lang = HFRequest.getIstance().getString(request, "new_lang");


		LocaleResolver localeResolver = RequestContextUtils.getLocaleResolver(request);
		if (localeResolver == null) {
			throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
		}

		LocaleEditor localeEditor = new LocaleEditor();
		localeEditor.setAsText(new_lang);
		localeResolver.setLocale(request, response, (Locale)localeEditor.getValue());


		HFResponse.getIstance().returnSuccess(response);

		return null;
	}

	/* =================验证码=================== */
	@RequestMapping("randomcode")
	public ModelAndView randomCode(HttpServletRequest request,HttpServletResponse response) throws Exception {
		WebRandomCodeUtils.productCodeImg(request, response);
		return null;
	}
	
	@RequestMapping("codeauth")
	public ModelAndView randomCodeAuth(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		if( !WebRandomCodeUtils.checkCode(request.getParameter("code"), request)) {
			HFResponse.getIstance().returnResult(response, -1, LoginResultType.VERIFICATION_CODE_ERROR.getValue(), null);
		}else {
			HFResponse.getIstance().returnSuccess(response);
		}
		
		return null;
	}
	
}
